<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/28
 * Time: 下午5:22
 */
namespace app\common\model;

use think\Cache;
use think\Exception;
use think\exception\DbException;
use think\Model;
use think\Db;
class Withdraw extends Common
{
    /**
     * 提现列表
     *
     */
    static public function withdrawList($user,$page,$page_size)
    {
        $list=Db::name('withdraw')
            ->where('uid',$user['id'])
            ->page($page,$page_size)
            ->order('id desc')
            ->field('id,create_time,money')
            ->select();

        return empty($list)?array():$list;

    }

    /**
     * 订单评价
     * @param  array $user 用户信息
     * @param array $info 联盟商信息
     * @param int $type 提现方式
     * @param string $account 提现账号
     * @param string $name 账号姓名
     * @param float $money 提现金额
     * @throws DbException
     * @throws DeveloperException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    static public function withdrawApply($user,$info,$type,$account,$name,$money)
    {
        $type = intval($type);
        $account = trim($account);
        $name = trim($name);
        $money = round($money,2);
        if(!in_array($type,[1,2,3])){
            throw new DeveloperException('提现方式有误！');
        }
        if($money<0.01 || $money > $user['money']){
            throw new DeveloperException('提现金额有误！');
        }

        //记录提现记录
        $percent = Db::name('rate')->where('id',1)->value('withdraw_rate');
        $service_charge = $money*$percent/100;
        $service_charge = round($service_charge,2);
        $da = [
            'uid'=>$user['id'],
            'union_id'=>$info['id'],
            'type'=>$type,
            'account'=>$account,
            'name'=>$name,
            'money'=>$money,
            'service_charge'=>$service_charge,
            'status'=>0,
            'create_time'=>now_datetime()
        ];
        $withdraw_id = Db::name('withdraw')->insertGetId($da);
        //给会员扣钱
        $data['money'] = $user['money']-$money;
        Db::name('user')->where('id',$user['id'])->update($data);
        //添加余额记录
        save_money_log($user['id'],$info['id'],3,-$money,$withdraw_id);
        return $withdraw_id;
    }

}

